//
//  RegisterView.m
//  iShanggang
//
//  Created by aiShangGang on 2017/3/31.
//  Copyright © 2017年 aishanggang. All rights reserved.
//

#import "RegisterView.h"
#import "UIColor+Hex.h"
//#import "UIViewExt.h"
#import "BOAssistor.h"
#import "BONoticeBar.h"
#import "BOHUDManager.h"
@interface RegisterView ()

@property (nonatomic,weak)UILabel *accontLbl;
@property (nonatomic,weak)UILabel *codeLbl;

@property (nonatomic,weak)UIView *accountView;
@property (nonatomic,weak)UIView *codeView;
@property (nonatomic,weak)UIView *longLineView1;
@property (nonatomic,weak)UIButton *getCodeBtn;
@property (nonatomic,weak)UIView *longLineView2;
@property (nonatomic,weak)UIButton *loginBtn;
@property (nonatomic,weak)UIButton *eyeBtn;
@property (nonatomic,weak)UIButton *registerBtn;
@property (nonatomic,weak)UIButton *protocalBtn;

@end

@implementation RegisterView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIView *accountV = [[UIView alloc] init];
        accountV.backgroundColor = [UIColor whiteColor];
        [self addSubview:accountV];
        self.accountView = accountV;
        
        UILabel *accountLbl = [self titleText:@"手机号码" view:accountV];
        self.accontLbl = accountLbl;
        UITextField *accountTextField = [self placeHolder:@"请输入手机号码" view:accountV];
        accountTextField.tag = 1;
        accountTextField.keyboardType = UIKeyboardTypeNumberPad;
        accountTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
        
        self.accountTextField = accountTextField;
        
        UIView *longLine1 = [[UIView alloc] init];
        longLine1.backgroundColor = [UIColor colorWithHex:0xF0F0F0];
        [self addSubview:longLine1];
        self.longLineView1 = longLine1;
        
        UIView *codeV = [[UIView alloc] init];
        codeV.backgroundColor = [UIColor whiteColor];
        [self addSubview:codeV];
        self.codeView = codeV;
        
        UILabel *codeLbl = [self titleText:@"验证码" view:codeV];
        self.codeLbl = codeLbl;
        UITextField *codeTextField = [self placeHolder:@"请输入验证码" view:codeV];
        codeTextField.tag = 2;
        codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        self.codeTextField = codeTextField;
        
        UIButton *getCodeBtn = [self title:@"获取验证码" color:0xB92D29 fontSize:14];
        [getCodeBtn addTarget:self action:@selector(didClickgetCodeBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:getCodeBtn];
        self.getCodeBtn = getCodeBtn;
        
        UIView *longLine2 = [[UIView alloc] init];
        longLine2.backgroundColor = [UIColor colorWithHex:0xF0F0F0];
        [self addSubview:longLine2];
        self.longLineView2 = longLine2;
       
        UIButton *registerBtn = [[UIButton alloc]init];
        registerBtn.backgroundColor = [UIColor colorWithHex:0xC4C4C4];
        [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        registerBtn.layer.cornerRadius = 5;
        registerBtn.layer.masksToBounds = YES;
        registerBtn.userInteractionEnabled = NO;
        [registerBtn addTarget:self action:@selector(registerAccount) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:registerBtn];
        self.registerBtn = registerBtn;
        
        NSString *protocalStr = @"注册即表示同意《爱上岗用户协议》";
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:protocalStr];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x1E75F7] range:NSMakeRange(7, 9)];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x999999] range:NSMakeRange(0, 7)];
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, protocalStr.length)];
        UIButton *protocalBtn = [[UIButton alloc] init];
        [protocalBtn setAttributedTitle:attrStr forState:UIControlStateNormal];
        [protocalBtn addTarget:self action:@selector(didClickProtocal:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:protocalBtn];
        self.protocalBtn = protocalBtn;
    }
    
    return self;
}

- (UIButton *)title:(NSString *)title color:(long)color fontSize:(int)fontSize{
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    [button setTitleColor:[UIColor colorWithHex:color] forState:UIControlStateNormal];
    [self addSubview:button];
    return button;
}

- (UITextField *)placeHolder:(NSString *)placeHolder view:(UIView *)passwordV {
    UITextField *textFiled = [[UITextField alloc]init];
    textFiled.placeholder = placeHolder;
    textFiled.textColor = [UIColor colorWithHex:0x666666];
    textFiled.font = [UIFont systemFontOfSize:14];
    [textFiled addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    [passwordV addSubview:textFiled];
    return textFiled;
}

- (UILabel *)titleText:(NSString *)titleText view:(UIView *)View {
    UILabel *label = [[UILabel alloc]init];
    label.text = titleText;
    label.textColor = [UIColor colorWithHex:0x333333];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:15];
    [View addSubview:label];
    return label;
}

#pragma mark - register
- (void)registerAccount {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"registerAccount" object:nil];
}

#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.accountView.frame = CGRectMake(0, 10, self.bounds.size.width, 44);
    [self.longLineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self);
        make.top.equalTo(self.accountView.mas_bottom);
        make.height.equalTo(@1);
    }];
    self.codeView.frame = CGRectMake(0, CGRectGetMaxY(self.accountView.frame) + 1, self.bounds.size.width, 44);
    [self.accontLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountView).offset(15);
        make.top.bottom.equalTo(self.accountView);
    }];
    
    [self.accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accontLbl.mas_right).offset(30);
        make.top.bottom.equalTo(self.accountView);
//        make.width.equalTo(@(self.width - self.accountTextField.left));
    }];
    
    [self.codeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.accontLbl);
        make.top.bottom.equalTo(self.codeView);
    }];
    
    [self.getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.codeView.mas_centerY);
        make.right.equalTo(self).offset(-15);
    }];
    
    [self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountTextField.mas_left);
        make.top.bottom.equalTo(self.codeView);
        make.width.equalTo(@(150));
    }];
    
    [self.longLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeView.mas_bottom);
        make.height.equalTo(@1);
        make.leading.equalTo(self.codeLbl);
        make.right.equalTo(self);
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.height.equalTo(@44);
        make.right.equalTo(self).offset(-15);
        make.top.equalTo(self.codeTextField.mas_bottom).offset(30);
    }];
    
    [self.protocalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self).offset(-10);
    }];
}

#pragma mark - button click
- (void)didClickProtocal:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"didClickProtocal" object:nil];
}

- (void)didClickgetCodeBtn {
    self.accountTextField.text = [self.accountTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (self.accountTextField.text.length > 0) {
        if (![BOAssistor phoneNumberIsValid:self.accountTextField.text]) {
            [BONoticeBar defaultBar].noticeText = @"请输入正确的手机号";
        }else{
            //获取验证码
            NSDictionary *pramas = [NSDictionary dictionaryWithObjectsAndKeys:self.accountTextField.text,@"mobile", nil];
            [[BOHUDManager defaultManager] progressHUDShowWithText:@"获取验证码"];
//            [ASGNetWorkEngine requestWithURL:getRegisterCodeURL sessionID:NO params:pramas completeBlock:^(NSDictionary *result) {
//                [[BOHUDManager defaultManager] progressHUDHideImmediately];
//
//                if ([result[@"status"]integerValue] == 0) {
//                    [[BONoticeBar defaultBar] setNoticeText:@"获取验证码成功"];
//                    [self timeChange];
//                }else{
//                    [[BONoticeBar defaultBar] setNoticeText:result[@"msg"]];
//                }
//                
//            } failedBlock:^(NSError *error) {
//                [[BOHUDManager defaultManager] progressHUDHideImmediately];
//            }];
        }
    } else {
        [BONoticeBar defaultBar].noticeText = @"请输入手机号";
    }
}

- (void)timeChange{
    [BOAssistor timeChangeWithBtn:self.getCodeBtn];
}

- (void)eyeBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
}

#pragma mark - screen touch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.accountTextField resignFirstResponder];
}

#pragma mark - textfield
- (void)changeText:(UITextField *)textfield {
    if (self.accountTextField.text.length > 0 && self.codeTextField.text.length >0 ) {
        self.registerBtn.backgroundColor = [UIColor colorWithHex:0xd23023];
        self.registerBtn.userInteractionEnabled = YES;
    } else {
        self.registerBtn.backgroundColor = [UIColor colorWithHex:0xC4C4C4];
        self.registerBtn.userInteractionEnabled = YES;
    }
}

@end
